<?php

namespace app\admin\controller\xyvenue;

use app\common\controller\Backend;
use app\admin\model\xyvenue\Article as ArticleModel;

/**
 * 链接管理
 *
 * @icon fa fa-circle-o
 */
class Link extends Backend
{

    /**
     * Link模型对象
     * @var \app\admin\model\xyvenue\Link
     */
    protected $model = null;
    protected $searchFields = 'id,name,type';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\xyvenue\Link;
        $this->view->assign("typeList", $this->model->getTypeList());
    }

    /**
     * 生成链接
     */
    public function load(){
        //基础链接
        $basicLink = [
            ['name'=>'主页','url'=>'/pages/index','type'=>'basic'],
            ['name'=>'场馆','url'=>'/pages/store','type'=>'basic'],
            ['name'=>'定场','url'=>'/pages/store/venue','type'=>'basic'],
            ['name'=>'切换场馆','url'=>'/pages/store/select','type'=>'basic'],
            ['name'=>'充值','url'=>'/pages/user/balance/recharge','type'=>'basic'],
        ];

        //会员中心
        $userLink = [
            ['name'=>'我的','url'=>'/pages/user','type'=>'user'],
            ['name'=>'我的订单','url'=>'/pages/user/order/list','type'=>'user'],
            ['name'=>'我的等级','url'=>'/pages/user/level','type'=>'user'],
            ['name'=>'我的包场','url'=>'/pages/user/package','type'=>'user'],
            ['name'=>'我的余额','url'=>'/pages/user/balance/detail','type'=>'user'],
            ['name'=>'我的资料','url'=>'/pages/user/info','type'=>'user'],
            
        ];
        
        //文章链接
        $articleLink = [];
        $articleList = ArticleModel::where(['status'=>'normal'])->select();
        foreach($articleList as $c){
            $articleLink[] = ['name'=>$c['title'],'url'=>'/pages/article/detail?id='.$c['id'],'type'=>'article'];
        }
        $allLink = array_merge($basicLink,$userLink,$articleLink);
        foreach($allLink as $l){
            $link = $this->model->where($l)->find();
            if(!$link){
                $this->model->create($l);
            }
        }
        $this->success('生成成功');
    }

    /**
     * 选择链接
     */
    public function select()
    {
        if ($this->request->isAjax()) {
            return $this->index();
        }
        return $this->view->fetch();
    }


}